package com.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.commons.dbutils.DbUtils;

import com.ui.panel.ExportSqlPanel;
import com.ui.panel.ExtendedPanel;

import cn.hutool.core.util.StrUtil;

public class Application extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel dsPanel;
	private JTextField hostText, portText, instanceText, userText;
	private JButton connectBtn;
	private ExtendedPanel mainPanel; // 扩展元数据面板
	private JTabbedPane tabbedPane; // 页签面板
	private ExportSqlPanel exportSqlPanel; // 导出脚本面板
	private JTextArea logText,logText2; // 日志文本域

	public static void main(String[] args) {
		new Application().setVisible(true);
	}

	public Application() {
		initUI();
	}

	public void initUI() {
		logger("开始初始化界面...");
		logger("加载扩展自定义项界面...");
		mainPanel = new ExtendedPanel(this, con);
		logger("加载扩展自定义项界面完成...");
		logger("加载导出脚本界面...");
		exportSqlPanel = new ExportSqlPanel(this, con);
		logger("加载导出脚本界面完成...");
		this.setTitle("YonyouTools");
		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(getDsPanel(), BorderLayout.NORTH);
		this.getContentPane().add(getTabbedPane());
		this.setSize(new Dimension(1250, 660));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		logger("初始化界面完成...");
	}

	private JTabbedPane getTabbedPane() {
		if (tabbedPane == null) {
			tabbedPane = new JTabbedPane();
			tabbedPane.addTab("导出脚本", null, exportSqlPanel, "Second");
			tabbedPane.addTab("扩展自定义项", null, mainPanel, "First");
			tabbedPane.addChangeListener(new ChangeListener() {
				
				@Override
				public void stateChanged(ChangeEvent e) {
					int selIndex = tabbedPane.getSelectedIndex();
					if (selIndex == 0) {
						getContentPane().getParent().setPreferredSize(new Dimension(650,500));
					} else {
						getContentPane().getParent().setPreferredSize(new Dimension(650,700));
					}
					getContentPane().getParent().repaint();
					
				}
			});
		}

		return tabbedPane;

	}

	/**
	 * 日志文本域
	 * 
	 * @return
	 */
	public JTextArea getLogText() {
		if (logText == null) {
			logText = new JTextArea(15, 100);
			logText.setEditable(false);
		}
		return logText;
	}
	
	public JTextArea getLogText2() {
		if (logText2 == null) {
			logText2 = new JTextArea(9, 100);
			logText2.setEditable(false);
		}
		return logText2;
	}

	private JPanel getDsPanel() {
		if (dsPanel == null) {
			dsPanel = new JPanel();
			TitledBorder titledBorder = BorderFactory.createTitledBorder("数据库信息");
			titledBorder.setTitleJustification(TitledBorder.LEFT);
			dsPanel.setBorder(titledBorder);
			dsPanel.add(new JLabel("Databse:"));
			dsPanel.add(getHostText());
			dsPanel.add(new JLabel("Username:"));
			dsPanel.add(getUserText());
			dsPanel.add(new JLabel("Password:"));
			dsPanel.add(getPwdText());
			dsPanel.add(getConnectBtn());

			dsPanel.setPreferredSize(new Dimension(650, 85));
		}

		return dsPanel;
	}

	public JTextField getHostText() {
		if (hostText == null) {
			hostText = new JTextField(40);
			hostText.setText("jdbc:oracle:thin:@10.16.5.69:1521/orcl.ufsoft.com.cn");
		}
		return hostText;
	}

	public JTextField getPortText() {
		if (portText == null) {
			portText = new JTextField(10);
		}
		return portText;
	}

	public JTextField getInstanceText() {
		if (instanceText == null) {
			instanceText = new JTextField(10);
		}
		return instanceText;
	}

	public JTextField getUserText() {
		if (userText == null) {
			userText = new JTextField(20);
			userText.setText("ncczr0401");
		}
		return userText;
	}

	private JPasswordField pwdText;

	public JPasswordField getPwdText() {
		if (pwdText == null) {
			pwdText = new JPasswordField(20);
			pwdText.setText("ncczr");
			pwdText.addFocusListener(new FocusListener() {

				@Override
				public void focusLost(FocusEvent e) {
					connect();

				}

				@Override
				public void focusGained(FocusEvent e) {
					// TODO Auto-generated method stub

				}
			});
		}
		return pwdText;
	}

	public JButton getConnectBtn() {
		if (connectBtn == null) {
			connectBtn = new JButton("测试连接");
			connectBtn.addActionListener(this);
		}
		return connectBtn;
	}

	private void connect() {
		try {
			logger("开始连接数据库...");
			if (con != null && !con.isClosed()) {
				con.close();
			}
			String url = getHostText().getText();
			String uname = getUserText().getText();
			String pwd = getPwdText().getText();
			if (StrUtil.isNotEmpty(url) && StrUtil.isNotEmpty(uname) && StrUtil.isNotEmpty(pwd)) {
				DbUtils.loadDriver("oracle.jdbc.driver.OracleDriver");
				con = DriverManager.getConnection(url, uname, pwd);
				logger("连接数据库成功...");
			} else {
				logger("数据库信息不完整不尝试连接...");
			}

		} catch (Exception e) {
			logger("连接数据库失败：" + e);
		}
	}

	/**
	 * 记录日志
	 * 
	 * @param message
	 */
	private void logger(String message) {
		getLogText().append(message);
		getLogText().append(System.lineSeparator());
		getLogText().setCaretPosition(getLogText().getText().length());
		getLogText2().append(message);
		getLogText2().append(System.lineSeparator());
		getLogText2().setCaretPosition(getLogText().getText().length());
	}

	private Connection con = null;

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.connectBtn) {
			connect();
		}
	}

}
